#include <netAddressChecker.h>
#include <iostream>

namespace View {

  bool NetAddressChecker::CheckIP(std::string IP)
  {
    std::string a,b,c,d;
    size_t pos = IP.find(".");
    if(pos == std::string::npos || 3 < pos)
      return false;
    a = IP.substr(0,pos);
    IP = IP.substr(pos +1);

    pos = IP.find(".");
    if(pos == std::string::npos || 3 < pos)
      return false;
    b = IP.substr(0,pos);
    IP = IP.substr(pos +1);

    pos = IP.find(".");
    if(pos == std::string::npos || 3 < pos)
      return false;
    c = IP.substr(0,pos);
    IP = IP.substr(pos +1);

    if(3 < IP.size() || 0 == IP.size())
      return false;
    d = IP;

    for(unsigned int i = 0; i < a.size(); i ++){
      if(48 > a[i] || 57 < a[i])
	return false;
    }
    for(unsigned int i = 0; i < b.size(); i ++){
      if(48 > b[i] || 57 < b[i])
        return false;
    }
    for(unsigned int i = 0; i < c.size(); i ++){
      if(48 > c[i] || 57 < c[i])
        return false;
    }
    
    for(unsigned int i = 0; i < d.size(); i ++){
      if(48 > d[i] || 57 < d[i])
        return false;
    }

    return true;
  }
  
  bool NetAddressChecker::CheckMAC(std::string MAC)
  {
    int i(2),j(0);
    int size = MAC.size();
    if(17 != size)
      return false;
    while(j < size){
      i--;
      if( (96 < MAC[j] && 123 > MAC[j]) ||
	  (64 < MAC[j] && 91 > MAC[j]) ||
	  (47 < MAC[j] && 58 > MAC[j]) )
	;
      else if(':' == MAC[j]){
	if(-1 != i)
	  return false;
	i = 2;
      }
      else
	return false;
      j++;
    }
    return true;
  }

  bool NetAddressChecker::CheckPort(std::string port)
  {
    if(htons(atoi(port.c_str()) ) == 0)
      return false;
    return true;
  }

}
